package edu.school.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.school.entity.BigData;
import edu.school.entity.Goods;
import edu.school.entity.GoodsExample;
import edu.school.mapper.GoodsMapper;
import edu.school.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper mapper;
    @Override
    public boolean save(Goods user) {
        return mapper.insertSelective(user)>0?true:false;
    }

    @Override
    public boolean update(Goods user) {
        return mapper.updateByPrimaryKeySelective(user)>0?true:false;
    }

    @Override
    public PageInfo<Goods> getPageList(Integer pageNum, Integer pageSize, Goods record) {
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria = example.createCriteria();
        if (null != record) {
            if (StringUtils.isNoneBlank(record.getGoods_name())) {//物品名称查询
                criteria.andGoods_nameLike("%" + record.getGoods_name() + "%");
            }
            if (StringUtils.isNoneBlank(record.getCreate_date())) {//日期查询
                criteria.andCreate_dateLike("%" + record.getCreate_date() + "%");
            }
            if (record.getU_id()!=null) {
                criteria.andU_idEqualTo(record.getU_id());
            }

        }
        PageHelper.startPage(pageNum, pageSize);//第1步
        List<Goods> list = mapper.selectByExample(example);//第2步

        PageInfo<Goods> info = new PageInfo<>(list);
        info.setPageNum(info.getPageNum());
        info.setPages(info.getPages());
        return info;

    }

    @Override
    public PageInfo<Goods> getQtPageList(Integer pageNum, Integer pageSize, Goods record) {
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(String.valueOf(0));
        if (null != record) {
            if (StringUtils.isNoneBlank(record.getGoods_name())) {//物品名称查询
                criteria.andGoods_nameLike("%" + record.getGoods_name() + "%");
            }
            if (StringUtils.isNoneBlank(record.getCreate_date())) {//日期查询
                criteria.andCreate_dateLike("%" + record.getCreate_date() + "%");
            }
            if (record.getU_id()!=null) {
                criteria.andU_idEqualTo(record.getU_id());
            }

        }
        PageHelper.startPage(pageNum, pageSize);//第1步
        List<Goods> list = mapper.selectByExample(example);//第2步

        PageInfo<Goods> info = new PageInfo<>(list);
        info.setPageNum(info.getPageNum());
        info.setPages(info.getPages());
        return info;
    }

    @Override
    public Goods findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean deleteGoods(Integer id) {
        return mapper.deleteByPrimaryKey(id)>0?true:false;
    }

    @Override
    public int deleteMany(String[] ids) {
        return mapper.deleteMany(ids);
    }

    @Override
    public List<BigData> selectBigData() {
        return mapper.selectBigData();
    }
}
